package org.sardine.aop;

import java.util.ArrayList;
import java.util.List;

import org.aopalliance.aop.Advice;
import org.apache.log4j.Logger;
import org.sardine.beans.BeanPostProcessor;

/**
 * 
 * 一个BeansAopProxyCreator能创建多个代理,通过setBeanNames()方法配置多个代理对象的别名
 * 
 * 
 * @author BearBear
 *
 */
public class BeansAopProxyCreator implements BeanPostProcessor{
	
	private final Logger log = Logger.getLogger(BeansAopProxyCreator.class);
	private List<String> beanNames = new ArrayList<String>();
	private List<Advice> interceptors = new ArrayList<Advice>();
	private List<String> methodNamePatterns = new ArrayList<String>();
	private boolean cglib = false;
	
	public Object postProcessAfterInitialization(String beanName, Object bean) {
		log.debug("postProcessAfterInitialization "+beanName+" and beanNames size is "+beanNames.size());
		
		/*
		int length = beanNames.size();
		if(length > 0){
			for(int i=0;i<length;i++){
				Class clazz = beanNames.get(i).getClass();
				log.info("class is "+clazz.getName());
			}
		}*/
		
		Object beanProxy = null;
		if(beanNames.contains(beanName)){
			log.info("开始创建id为\""+beanName+"\"的实例的代理");
			AopProxyCreator proxyCreator = new AopProxyCreator(bean,cglib);
			proxyCreator.setInterceptors(interceptors);
			proxyCreator.setMethodNamePatterns(methodNamePatterns);
			proxyCreator.setUseFullyQualifiedName(false);
			beanProxy = proxyCreator.getProxy();
			
		}else{
			return bean;
		}
		
		
		return beanProxy;
	}
	
	public void addBeanName(String beanName){
		this.beanNames.add(beanName);
	}

	public List<String> getBeanNames() {
		return beanNames;
	}

	public void setBeanNames(List<String> beanNames) {
		this.beanNames = beanNames;
	}

	public List<Advice> getInterceptors() {
		return interceptors;
	}

	public boolean isCglib() {
		return cglib;
	}

	public void setInterceptors(List<Advice> interceptors) {
		this.interceptors = interceptors;
	}

	public void setCglib(boolean cglib) {
		this.cglib = cglib;
	}

	public List<String> getMethodNamePatterns() {
		return methodNamePatterns;
	}

	public void setMethodNamePatterns(List<String> methodNamePatterns) {
		this.methodNamePatterns = methodNamePatterns;
	}
	
	

}
